package com.wn;

import com.wn.entity.Student;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yellow Docter
 * @date 2023 -03-03
 * @desc 测试搜索的操作
 * 1、QueryBuilders构建 查询条件
 * 2、通过查询条件构建本地查询 NativeSearchQuery
 * 3、调用search方法解析查询结果 
 */
@SpringBootTest
public class ESTest02 {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    //通过名称搜索
    @Test
    public void demo01() {

    //        QueryBuilders.queryStringQuery();   字符串查询
    //        QueryBuilders.boolQuery()           bool查询
    //        QueryBuilders.matchAllQuery()       查询所有

        QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("手机");
        Query query = new NativeSearchQuery(queryBuilder);
        SearchHits<Student> searchHits = elasticsearchRestTemplate.search(query, Student.class);

        List<Student> students = searchHits.stream().map(hit -> hit.getContent()).collect(Collectors.toList());
        System.out.println("students = " + students);


    }

    /**
     * 搜索所有
     */
    @Test
    public void addStu() {

        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        Query query = new NativeSearchQuery(queryBuilder);

        //拿到所有的查询的hits
        SearchHits<Student> searchHits = elasticsearchRestTemplate.search(query, Student.class);
        System.out.println("searchHits = " + searchHits);

        // 拿到我们的 每一个 hits的集合
        List<SearchHit<Student>> hits = searchHits.getSearchHits();
        System.out.println("hits = " + hits);
        //拿到每一个hits下面的  _source 下面的 数据
        hits.forEach(hit-> System.out.println("hit = " + hit.getContent()));

    }

    @Test
    public void searchStus() {

        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        Query query = new NativeSearchQuery(queryBuilder);

        //拿到所有的查询的hits
        SearchHits<Student> searchHits = elasticsearchRestTemplate.search(query, Student.class);
        
        // 拿到我们的 每一个 hits的集合
        List<SearchHit<Student>> hits = searchHits.getSearchHits();

        List<Student> students = hits.stream()      //拿到流
                .map(hit -> hit.getContent())       //把每一个 hit 转成  hit.getContent()中的student
                .collect(Collectors.toList());      //收集

        System.out.println("students = " + students);
    }

}
